package ClassTest.JDBCtest.dao;


import ClassTest.JDBCtest.dao.impl.JDBCImpl;
import ClassTest.JDBCtest.domain.Nember;

import java.sql.*;
import java.util.Scanner;

public class JDBCDao implements JDBCImpl {
    private Connection conn;
    private PreparedStatement ps;
    private Scanner sc = new Scanner(System.in);

    public JDBCDao() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        String username = "root";
        String password = "root";
        conn = DriverManager.getConnection(url, username, password);
    }

    //增加
    @Override
    public boolean insert(Nember nember) {
        int i = 0;
        try {
            String sql = "INSERT INTO student VALUES (?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, nember.getId());
            ps.setString(2, nember.getName());
            ps.setInt(3, nember.getAge());
            ps.setString(4, nember.getAddress());
            ps.setString(5, nember.getPhone());
            i = ps.executeUpdate();
        } catch (Exception e) {
        }
        return i!=0;
    }

    //删除
    @Override
    public boolean delete(int n) {
        String sql = null;
        try {
            if (n == 1) {
                System.out.println("请输入您要删除的成员ID:");
                String id = sc.nextLine();

                if (select(id, true)) {
                    System.out.println("ID不存在,请重新输入！");
                    return false;
                }

                sql = "delete from student where id=?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, id);
            } else if (n == 2) {
                sql = "delete from student";
                ps = conn.prepareStatement(sql);
            }
            int count = ps.executeUpdate();
            if (count == 0) {
                return false;
            }
            System.out.println("共删除" + count + "条记录!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    //改
    @Override
    public boolean update(Nember nember) {
        try {
            String sql = "update student set name=?,age=?,address=?,phone=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, nember.getName());
            ps.setInt(2, nember.getAge());
            ps.setString(3, nember.getAddress());
            ps.setString(4, nember.getPhone());
            ps.setString(5, nember.getId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    //查找
    @Override
    public boolean select(String i, Boolean isreturn) {
        int f = 0;
        try {
            String sql;
            if (i.equals("*")) {
                sql = "select * from student";
                ps = conn.prepareStatement(sql);
            } else {
                sql = "select * from student where id=?";
                ps = conn.prepareStatement(sql);
                ps.setString(1,i);
            }
            ResultSet s = ps.executeQuery();
            //不返回（实际上是有些调用者不需要打印，只需要返回判断）
            while (s.next()) {
                if (isreturn == false) {
                    System.out.println("\033[1;40m"+
                            "ID:" + s.getString("id") + "," +
                            "姓名:" + s.getString("name") + "," +
                            "年龄:" + s.getInt("age") + "," +
                            "地址:" + s.getString("address") + "," +
                            "电话:" + s.getString("phone")+"\033[0m");
                }
                f = 1;
            }
            if (f == 0 && isreturn == false) {
                System.out.println("数据不存在！");
            }
        } catch (SQLException e) {
            System.out.println("打印错误");
        }
        return f == 1;
    }

    //关闭
    public void close() {
        try {
            if (conn != null) {
                conn.close();
            }
            if (ps != null) {
                ps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
